package com.qf.commerce.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.commerce.common.dto.UserDto;
import com.qf.commerce.common.dto.UserSelectDto;
import com.qf.commerce.common.vo.UserVo;
import com.qf.commerce.entity.User;
import com.qf.commerce.exception.ControllerException;
import com.qf.commerce.exception.DaoException;
import com.qf.commerce.exception.ServiceException;
import com.qf.commerce.mapper.UserMapper;
import com.qf.commerce.resp.ResponseStatus;
import com.qf.commerce.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/*
 * @author 358
 * @since JDK 1.8
 * @data 2023/9/11 17:14
 */

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public int deleteByPrimaryKey(Integer userId) {
        return userMapper.deleteByPrimaryKey(userId);
    }

    @Override
    public int insert(UserDto record) {
        User user = new User();
        if (ObjectUtils.isEmpty(user) || ObjectUtils.isEmpty(record)) {
            return 0;
        }
        BeanUtils.copyProperties(record, user);
        return userMapper.insert(user);
    }

    @Override
    public User selectByPrimaryKey(Integer userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public PageInfo<UserVo> selectAll(Integer page, Integer size) {
        return PageHelper.startPage(page, size).doSelectPageInfo(() -> userMapper.selectAll());
    }

    @Override
    public UserVo selectById(Integer userId) {
        if (userId == null) {
            throw new ControllerException(ResponseStatus.DATA_ERROR);
        } else {
            UserSelectDto userSelectDto = new UserSelectDto();
            userSelectDto.setUserId(userId);
            UserVo userVo = userMapper.select(userSelectDto);
            if (userVo == null) {
                throw new DaoException(ResponseStatus.SYS_ERROR);
            }
            return userVo;
        }
    }

    @Override
    public int update(UserDto userDto) {
        User user = new User();
        if (ObjectUtils.isEmpty(user) || ObjectUtils.isEmpty(userDto)) {
            throw new ServiceException(ResponseStatus.DATA_ERROR);
        }
        BeanUtils.copyProperties(userDto, user);
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public int updateBalance(User user) {
        int num = userMapper.updateByPrimaryKeySelective(user);
        if (num > 0) {
            return num;
        } else throw new DaoException(ResponseStatus.SYS_ERROR);
    }

    @Override
    public Integer updateStatus(Integer userId, Integer status) {
        User user = new User();
        user.setUserId(userId);
        user.setStatus(status);
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public PageInfo<UserVo> select(UserSelectDto userSelectDto) {
        if(ObjectUtils.isEmpty(userSelectDto)){
            throw new ControllerException(ResponseStatus.DATA_ERROR);
        }
        return PageHelper.startPage(userSelectDto.getPage(), userSelectDto.getSize()).doSelectPageInfo(() -> userMapper.select(userSelectDto));
    }
}
